/**
 * 登录服务类
 *  */
import Request from './Ajax'
import sha1 from '../lib/sha1'
class userLogin {
  static async user(parameter, layer) {
    const url = `Index/login`
    console.log(parameter)
    let user_code = await Request.post(url, parameter, true)
    if (user_code == 121) {
      layer.msg('用户名不存在，请联系管理员！', {
        icon: 3,
        time: 700,
        offset: 't'
      });
      return false;
    } else if (user_code == 123) {
      layer.msg('密码不正确，请重新输入！', {
        icon: 4,
        time: 700,
        offset: 't'
      });
      return false;
    } else if (user_code == 0) {
      layer.msg('登录成功！', {
        icon: 1,
        time: 700,
        offset: 't',
        success: function() {
          // 存储用户信息
          window.sessionStorage.setItem('user_info', parameter);
          setTimeout(() => {
            window.location.href = "../../index.html"
          }, 700)
        }
      });
    } else {
      console.log('未知错误！')
    }
  }
}
layui.define(['form'], function(exports){
  var form = layui.form;
  var layer = layui.layer;
  //监听提交
  form.on('submit(formDemo)', function(data){
    let parameter = `username=${data.field.username}&password=${sha1.hex_sha1(data.field.password)}`
    userLogin.user(parameter, layer)
    return false
  });
  exports('login', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});